package
{
	import Map.CMapData;
	
	public class CGameStageManager
	{
		private static var m_instance :CGameStageManager = new CGameStageManager();
		private var m_script :CSprite_GameScript;
		private var m_arrScript :Array;
		private var m_MapData :CMapData;
		private var m_nCurrentStageNumber :uint;
		private var m_nStageCount :uint;
		private var m_nMapSize_X :uint;
		private var m_nMapSize_Y :uint;
		
		public static function get instance() :CGameStageManager { return m_instance; }
		public function get refMapData() :CMapData { return m_MapData; }
		public function get nStageCount() :uint { return m_nStageCount; }
		public function get nCurrentStageNumber() :uint { return m_nCurrentStageNumber; }
		public function get nMapSizeX() :uint { return m_nMapSize_X; }
		public function get nMapSizeY() :uint { return m_nMapSize_Y; }
		
		public function CGameStageManager()
		{
			m_script = new CSprite_GameScript();
			m_arrScript = new Array();
			m_MapData = new CMapData();
			m_nCurrentStageNumber = 0;
			
			m_nStageCount = uint(m_script.scriptStageCount.text);
			m_nMapSize_X = uint(m_script.scriptMapSize_X.text);
			m_nMapSize_Y = uint(m_script.scriptMapSize_Y.text);
			
			m_arrScript.push(m_script.scriptStage01.text);
			m_arrScript.push(m_script.scriptStage02.text);
			m_arrScript.push(m_script.scriptStage03.text);
			m_arrScript.push(m_script.scriptStage04.text);
			m_arrScript.push(m_script.scriptStage05.text);
		}
		public function Clear() :void
		{
			ClearMapData();
			m_nCurrentStageNumber = 0;
		}
		public function ClearMapData() :void
		{
			m_MapData.Clear();
		}
		public function LoadMapScript() :Boolean
		{
			ClearMapData();
			
			var arrRowData :Array = m_arrScript[m_nCurrentStageNumber].split(".");
			for (var i :uint = 0; i < arrRowData.length; ++i)
			{
				var arrColData :Array = arrRowData[i].split(",");
				
				m_MapData.Push(arrColData);
			}
			
			if (m_MapData.nMapSizeX != m_nMapSize_X || m_MapData.nMapSizeY != m_nMapSize_Y)
			{
				trace("Failed! CGameStageManager::LoadMapScript()" + m_MapData.nMapSizeX + ", " + m_MapData.nMapSizeY);
				return false;
			}
			
			return true;
		}
		public function GetMapType(nMapPosX :int, nMapPosY :int) :uint
		{
			return m_MapData.GetMapTileType(nMapPosY, nMapPosX);
		}
		public function NextStage() :void
		{
			m_nCurrentStageNumber++;
		}

	}
}